(function($, window, document,undefined) {
		var data=[];
		var methods={
			getSetting:function(){
				return settings;
			},
			getSelected:function(){
				return data;
			},
			init:function(options){
				
				var defaults={
				startTime:null,
				endTime:null,
				stadiumID:null,
				currDate:null,
				places:null,
				maxCount:0,
				status:{'0':'',"1":'已预约',"2":'关闭'}
				}
				var settings= $.extend(defaults, options);
				var diffHours=function(obj){
					var now=new Date();
					var start=new Date(now.getFullYear()+"/"+now.getMonth()+"/"+now.getDay()+" "+obj.startTime);
					var end=new Date(now.getFullYear()+"/"+now.getMonth()+"/"+now.getDay()+" "+obj.endTime);
					var diff=end.getTime()-start.getTime();
					var diffHours=diff%(24*3600*1000);
					diffHours=diffHours/(3600*1000);
					return {start:start,end:end,diff:diff,diffHours:diffHours};
				}
				var rendHours=function(obj){
					var ul=$("<ul></ul>").addClass("time");
					for(var i=0;i<=obj.diffHours;i++){
						var hour=obj.start.getHours();
						var minutes=obj.start.getMinutes();
						hour=hour+i+":"+(minutes<10?"0"+minutes:minutes);
						var li=$("<li></li>").text(hour);
						ul.append(li);
					}
					this.append(ul);
				}
				var rendPlace=function(dom,p){
					$(p).each(function(i){
						var ul=$("<ul></ul>").addClass("place");
						ul.append($("<li></li>").addClass("place-name").text(this.name));
						var pObj=this;
						$(this.schdule).each(function(pi){
							var diffObj=diffHours(this);
							var width=(diffObj.diffHours*50+(diffObj.diffHours-1)*2)+"px";
							var li=$("<li></li>");
							if(this.status=='0'){
								this.name=pObj.name;
								var title=" "+pObj.name+" "+this.startTime+"-"+this.endTime+" "+accounting.formatMoney(this.price,"￥");
								li.text(this.price).addClass("place-price").attr("status","0").attr("title",title);
							}
							else{
								li.text(settings.status[this.status]).addClass("place-unuseable");
							}
							li.data("rawdata",this).css({width:width,textAlign:"center"}).appendTo(ul);
						});
						dom.append(ul);
					})
				}
				var putOrRemove=function(o){
					var put=true;
					$(data).each(function(i){
						if(this.id==o.id)
						{
							put=false;
							data.splice(i,1)
							return;
						}
					})
					if(put){
						if(settings.maxCount==0||data.length<settings.maxCount){
							data.push(o);
						}
						else{
							thisObj.trigger("overflow",data);
							return false;
						}
					}
					return put;
				}
				rendHours.call(this,diffHours(settings));
				rendPlace(this,settings.places);
				
				this.on("click",function(e){
					var ele=e.target;
					var sta=$(ele).attr("status");
					if(sta=="0"){
						var d=$(ele).data("rawdata");
						var re=putOrRemove(d);
						if(re){
							$(ele).addClass("place-selected");
							thisObj.trigger("selectPlace",d);
						}
						else{
							$(ele).removeClass("place-selected");
							thisObj.trigger("cancelPlace",d);
						}
					}
				});
				
			}
		}

	var thisObj;
    $.fn.stadiumSchdule = function(options) {
		thisObj=this;
		var method = arguments[0];
		if(methods[method]) {
			method = methods[method];
			arguments = Array.prototype.slice.call(arguments, 1);
		} else if( typeof(method) == 'object'  ) {
			method = methods.init;
		} else {
			$.error( 'Method ' +  method + ' does not exist on stadiumSchdule' );
			return this;
		}
		// 用apply方法来调用我们的方法并传入参数
		return method.apply(this, arguments);
    }
})(jQuery, window, document);